1.设置bundle name

选中assets文件,在右下角面板中为其设置assets bundle的名字

2.打包,会自动打包依赖文件

核心代码:

1
BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath+ "/Assetbundle"BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.Android);

路径随意,注意BuildTarget平台选择。

然后将其放入Editor文件夹下的脚本文件中,作为editor扩展命令:

1
2
3
4
5
6
7
[MenuItem("BuildAssetbundle/BuildAll")]
static void Build()
{
BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath+ "/Assetbundle", BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.Android);
EditorUtility.DisplayDialog("提示", "导出完成", "确定");
}

3.Android相关路径备注:

1
2
3
4
Application.persistentDataPath = /storage/emulated/0/Android/data/com.moxiu.demo/files
Application.dataPath = /data/app/com.moxiu.demo-2.apk
Application.streamingAssetsPath = jar:file:///data/app/com.moxiu.demo-2.apk!/assets
Application.temporaryCachePath = /storage/emulated/0/Android/data/com.moxiu.demo/cache

4.加载assets bundle示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private string baseUrl = "http://10.0.102.45:8080/Assetbundle/";
private string basePath = "/sdcard/Assetbundle/";
public GameObject LoadFromLoacl (string bundleName, string objectName)
{
AssetBundle targetBundle = AssetBundle.LoadFromFile (basePath + bundleName);
if (targetBundle != null) {
GameObject asset = targetBundle.LoadAsset (objectName) as GameObject;
targetBundle.Unload (false);
return asset;
} else {
return null;
}
}
public GameObject LoadFromNet (string bundleName, string objectName)
{
WWW www = WWW.LoadFromCacheOrDownload (baseUrl + bundleName, 1);
AssetBundle targetBundle = www.assetBundle;
if (targetBundle != null) {
GameObject asset = targetBundle.LoadAsset (objectName) as GameObject;
targetBundle.Unload (false);
return asset;
} else {
return null;
}
}

EOF